using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Husb.Util;

namespace Husb.WinForm.Controls
{
    public class DataGridViewEnumCell : DataGridViewTextBoxCell
    {
        public DataGridViewEnumCell() { }
        public DataGridViewEnumCell(string enumTypeFullName)
        {
            this.enumTypeFullName = enumTypeFullName;
        }
        private string enumTypeFullName;
        public string EnumTypeFullName
        {
            get 
            {
                //base.pr
                if (enumTypeFullName == null || enumTypeFullName.Length == 0)
                {
                    //return "Husb.Common.Util.LogType";
                    return "";
                    //throw new ArgumentException("Invalid enumTypeFullName, EnumTypeFullName must input");
                }
                return enumTypeFullName; 
            }
            set { enumTypeFullName = value; }
        }

        //protected override AccessibleObject CreateAccessibilityInstance()
        //{
        //    return base.CreateAccessibilityInstance();
        //}
        protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
        {
            object o = base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);

            DataGridViewEnumColumn emunColumn = (DataGridViewEnumColumn)OwningColumn;
            enumTypeFullName = emunColumn.EnumTypeFullName;
            if (o.ToString().Length == 0)
            {
                return "";
            }


            return EnumUtil.GetDisplayName(enumTypeFullName, int.Parse(o.ToString()));
        }

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);

            TextBox textBox = base.DataGridView.EditingControl as TextBox;

            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            DataGridViewTextBoxEditingControl box = DataGridView.EditingControl as DataGridViewTextBoxEditingControl;
            //DataGridViewDataWindowEditingControl dataWindowControl = DataGridView.EditingControl as DataGridViewDataWindowEditingControl;
            //dataWindowControl.PopupGridAutoSize = true;
            //dataWindowControl.RowFilterVisible = false;
            DataGridViewEnumColumn emunColumn = (DataGridViewEnumColumn)OwningColumn;

            enumTypeFullName = emunColumn.EnumTypeFullName;

            box.Text = (string)this.Value;

            //DataGridViewDataWindowColumn dataWindowColumn = (DataGridViewDataWindowColumn)OwningColumn;
            //dataWindowControl.DataSource = dataWindowColumn.DataSource;
            //dataWindowControl.Text = (string)this.Value;


        }

        protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
        }

        public override Type FormattedValueType
        {
            get
            {
                return base.FormattedValueType;
            }
        }

        protected override object GetValue(int rowIndex)
        {
            return base.GetValue(rowIndex);
        }

        public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter formattedValueTypeConverter, System.ComponentModel.TypeConverter valueTypeConverter)
        {
            return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
        }

        public override void PositionEditingControl(bool setLocation, bool setSize, System.Drawing.Rectangle cellBounds, System.Drawing.Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
        {
            base.PositionEditingControl(setLocation, setSize, cellBounds, cellClip, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow);
        }

        protected override bool SetValue(int rowIndex, object value)
        {
            return base.SetValue(rowIndex, value);
        }

        public override Type EditType
        {
            get
            {
                return base.EditType;
            }
        }

        public override Type ValueType
        {
            get
            {
                return base.ValueType;
            }
        }
    }
}
